unit ObjectsUnit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Contnrs;

type

{ TExecutableVersionContainer }

  TExecutableVersionContainer = class
  private
    FMajor: Integer;
    FMinor: Integer;
    FRevision: Integer;
    FBuild: Integer;

    function GetVersion: String;
  protected
  public
    constructor Create;

    procedure FromJSON(AJSON: String);
    function ToJSON: String;

    property Major: Integer read FMajor write FMajor;
    property Minor: Integer read FMinor write FMinor;
    property Revision: Integer read FRevision write FRevision;
    property Build: Integer read FBuild write FBuild;
    property Version: String read GetVersion;
  end;

{ TContentType }

  TContentType = (ctText, ctHTML, ctTOC, ctNone);

{ TContentItemContainer }

  TContentItemContainer = class
  private
    FVersion: Integer;
    FID: String;
    FName: String;
    FContentType: TContentType;
    FPath: String;
    FFileName: String;
  protected
  public
    constructor Create;

    property Version: Integer read FVersion write FVersion;
    property ID: String read FID write FID;
    property Name: String read FName write FName;
    property ContentType: TContentType read FContentType write FContentType;
    property Path: String read FPath write FPath;
    property FileName: String read FFileName write FFileName;
  end;

{ TContentContainer }

  TContentContainer = class
  private
    FContentItems: TFPObjectList;

    function GetCount: Integer;
    function GetItems(Index: Integer): TContentItemContainer;
  protected
  public
    constructor Create;
    destructor Destroy; override;

    function Add(AItem: TContentItemContainer): Integer;
    procedure Delete(AIndex: Integer);

    procedure FromJSON(AJSON: String);
    function ToJSON: String;

    property Count: Integer read GetCount;
    property Items[Index: Integer]: TContentItemContainer read GetItems; default;
  end;

implementation

uses
  fpJSON;

{ TContentItemContainer }

constructor TContentItemContainer.Create;
begin
  FVersion:= 0;
  FID:= '';
  FName:= '';
  FContentType:= ctNone;
  FPath:= '';
  FFileName:= '';
end;

{ TContentContainer }

constructor TContentContainer.Create;
begin
  FContentItems:= TFPObjectList.Create(True);
end;

destructor TContentContainer.Destroy;
begin
  //FContentItems.Clear;
  FContentItems.Free;
  inherited Destroy;
end;

function TContentContainer.GetCount: Integer;
begin
  Result:= FContentItems.Count;
end;

function TContentContainer.GetItems(Index: Integer): TContentItemContainer;
begin
  Result:= TContentItemContainer(FContentItems[Index]);
end;

function TContentContainer.Add(AItem: TContentItemContainer): Integer;
begin
  Result:= FContentItems.Add(AItem);
end;

procedure TContentContainer.Delete(AIndex: Integer);
begin
  FContentItems.Delete(AIndex);
end;

procedure TContentContainer.FromJSON(AJSON: String);
begin
  if Length(AJSON) = 0 then exit;
end;

function TContentContainer.ToJSON: String;
var
  JSONObj, ContentItemObj: TJSONObject;
  ContentArrObj: TJSONArray;
  Index: Integer;
  ContentItem: TContentItemContainer;
begin
  Result:= '';
  JSONObj:= TJSONObject.Create;
  ContentArrObj:= TJSONArray.Create;
  JSONObj.Add('content', ContentArrObj);
  for Index:= 0 to FContentItems.Count - 1 do
  begin
    ContentItem:= TContentItemContainer(FContentItems.Items[Index]);
    ContentArrObj.Add(ContentItem.ID);
    ContentItemObj:= TJSONObject.Create;
    ContentItemObj.Add('version', ContentItem.Version);
    ContentItemObj.Add('name', ContentItem.name);
    ContentItemObj.Add('type', Ord(ContentItem.ContentType));
    ContentItemObj.Add('path', ContentItem.Path);
    ContentItemObj.Add('file', ContentItem.FileName);
    JSONObj.Add(ContentItem.ID, ContentItemObj);
  end;
  Result:= JSONObj.FormatJSON([foUseTabchar], 1);
  JSONObj.Free;
end;

{ TExecutableVersionContainer }

function TExecutableVersionContainer.GetVersion: String;
begin
  Result:= IntToStr(FMajor)+'.'+IntToStr(FMinor)+'.'+
           IntToStr(FRevision)+'.'+IntToStr(FBuild);
end;

constructor TExecutableVersionContainer.Create;
begin
  FMajor:= 0;
  FMinor:= 0;
  FRevision:= 0;
  FBuild:= 0;
end;

procedure TExecutableVersionContainer.FromJSON(AJSON: String);
begin
  if Length(AJSON) = 0 then exit;
end;

function TExecutableVersionContainer.ToJSON: String;
var
  JSONObj: TJSONObject;
  VersionObj: TJSONObject;
begin
  Result:= '';
  JSONObj:= TJSONObject.Create;
  VersionObj:= TJSONObject.Create;
  VersionObj.Add('major', FMajor);
  VersionObj.Add('minor', FMinor);
  VersionObj.Add('revision', FRevision);
  VersionObj.Add('build', FBuild);
  JSONObj.Add('version', VersionObj);
  Result:= JSONObj.FormatJSON([foUseTabchar],1);
  JSONObj.Free;
end;

end.

